package org.deng.vertx.springboot.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.deng.vertx.springboot.entity.BaseEntity;
import org.deng.vertx.springboot.service.IBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author 邓力宾 榆霖
 * @date 2022/5/10 9:21
 * @desc 日期处理工具类
 */
public class BaseServiceImp<M extends BaseMapper<T>, T extends BaseEntity>
        extends ServiceImpl<M, T> implements IBaseService<T> {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    //逻辑删除字段
    private static final String DELETE_COLUMN = "deleted";

    @Override
    public void logicDel(Number id) {
        UpdateWrapper<T> wrapper = Wrappers.update();
        wrapper.eq("id", id).set(DELETE_COLUMN, true);
        this.update(wrapper);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if(ids == null || ids.isEmpty()){
            return;
        }
        QueryWrapper<T> wrapper = Wrappers.query();
        wrapper.in("id", ids);
        this.remove(wrapper);
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return 对象
     */
    @Override
    public T get(Number id) {
        if (id == null) {
            return null;
        }
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq(DELETE_COLUMN, false);
        return this.getOne(wrapper);

    }

    /**
     * 根据多个id查询
     *
     * @param ids
     * @return
     */
    @Override
    public List<T> getByIds(Collection<? extends Number> ids) {
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        queryWrapper.eq(DELETE_COLUMN, false);
        queryWrapper.orderByAsc("id");
        return this.list(queryWrapper);
    }

    /**
     * 简单分页查询 根据id排序
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<T> getPage(int pageNum, int pageSize) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DELETE_COLUMN, false);
        queryWrapper.orderByAsc("id");
        return this.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    /**
     * 查询所有按id排序
     *
     * @return
     */
    @Override
    public List<T> getAll() {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DELETE_COLUMN, false);
        queryWrapper.orderByAsc("id");
        return this.list(queryWrapper);
    }

    /**
     * 添加
     *
     * @param t
     * @return
     */
    @Override
    public T add(T t) {
        baseMapper.insert(t);
        return t;
    }

    @Override
    public Long addGetId(T t) {
        return add(t).getId();
    }

    @Override
    public Map<Long, T> getAllMap() {
        List<T> all = getAll();
        return all.stream().collect(Collectors.toMap(T::getId, i->i));
    }
}

